//Controls 'disabled' attributes subtree, headed with 'entity' element. 
//If state == true, 'disabled = true' is set to all elements in subrtee
//If state == false, 'disabled' arrtibute is removed
gOneClickExtension.SetDisabledHierarchy = function( entity, state )
{
	if ( !entity ) return;

	var x = entity.firstChild;
	while ( x )
	{
		gOneClickExtension.SetDisabledHierarchy( x, state );

		if ( state )
			x.removeAttribute( 'disabled' );
		else
			x.setAttribute( 'disabled', true );

		x = x.nextSibling;
    }
		if ( state )
			entity.removeAttribute( 'disabled' );
		else
			entity.setAttribute( 'disabled', true );
}

//Load preferences from preferences DB and init all GUI controls
gOneClickExtension.InitPreferencesDialog = function()
{
	var prefs = gOneClickExtension.LoadPreferences();
	var modifiersGroup = document.getElementById("ocsModifierGroup");
	
	if ( prefs.selectionType == "alt" )
		modifiersGroup.selectedItem = document.getElementById("ocsAlt");
	else if ( prefs.selectionType == "ctrl" )
		modifiersGroup.selectedItem = document.getElementById("ocsCtrl");
	else if ( prefs.selectionType == "shift" )
		modifiersGroup.selectedItem = document.getElementById("ocsShift");
	else; //TODO: print error!

    document.getElementById("ocsMiddleButton").checked = prefs.enableMiddleButton;
	document.getElementById("ocsEnableRegexp").checked = prefs.enableRegexp;
	gOneClickExtension.SetDisabledHierarchy( document.getElementById("ocsRegexpGrid"), prefs.enableRegexp );

	document.getElementById("ocsMatch").value          = prefs.regexpMatch;
	document.getElementById("ocsReplace").value        = prefs.regexpReplace;
    
}

//Get preferences from GUI controls and save them to preferences DB
gOneClickExtension.SavePreferencesFromDialog = function()
{
	var prefs = {};
	if ( document.getElementById("ocsAlt").selected )
		prefs.selectionType = "alt";
	else if ( document.getElementById("ocsCtrl").selected )
		prefs.selectionType = "ctrl";
	else if ( document.getElementById("ocsShift").selected )
		prefs.selectionType = "shift";
	else; //TODO: print error!

    prefs.enableMiddleButton = document.getElementById("ocsMiddleButton").checked;
	prefs.enableRegexp       = document.getElementById("ocsEnableRegexp").checked;
	prefs.regexpMatch        = document.getElementById("ocsMatch"       ).value;
	prefs.regexpReplace      = document.getElementById("ocsReplace"     ).value;

	gOneClickExtension.SavePreferences( prefs );

	return true;
}